<template>
  <div class="app-container">
    <el-form
      v-loading="infoLoading"
      label-width="0"
      :inline-message="true"
    >
      <el-form-item>
        <el-table :data="list">
          <el-table-column label="提醒服务" min-width="60">
            <template slot-scope="scope">
              {{ scope.row.title }}
            </template>
          </el-table-column>
          <el-table-column align="center" label="开启/关闭">
            <template slot-scope="scope">
              <el-switch
                v-model="scope.row.is_open"
                @change="switchButton(scope.row.id)"
              />
            </template>
          </el-table-column>
          <el-table-column label="提醒人列表" min-width="420">
            <template slot-scope="scope">
              <el-select
                v-if="scope.row.dialogFormVisible && scope.row.is_open === true"
                v-model="scope.row.tag"
                multiple
                filterable
                allow-create
                default-first-option
                placeholder="请选择"
                :multiple-limit="10"
                style="width:380px"
              >
                <el-option
                  v-for="item in options"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                />
              </el-select>
              <span v-if="scope.row.dialogFormVisible && scope.row.is_open === true" class="maxlen"><b :class="scope.row.tag.length > 10 ? 'colors' : '' ">{{ scope.row.tag.length }}</b>/10</span>
              <el-button v-if="scope.row.choices === true && scope.row.is_open === true" class="choice" type="primary" @click="choice(scope.row.id)">选择</el-button>
              <el-button v-if="scope.row.dialogFormVisible && scope.row.is_open === true" class="preservation" type="primary" @click="preservation(scope.row.id,scope.row.tag)">保存</el-button>
              <el-button v-if="scope.row.dialogFormVisible && scope.row.is_open === true" class="cancel" type="primary" @click="cancel(scope.row.id)">取消</el-button>
              <span v-for="admin in scope.row.admin_name" v-if="scope.row.choices" class="span">{{ admin }}</span>
            </template>
          </el-table-column>
        </el-table>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { switchButton, setToDoNotification, getToDoNotification, getAdminList } from '@/api/configuration'

export default {
  props: ['listtype'],
  data() {
    return {
      options: [],
      infoLoading: true,
      list: [],
      dialogTitle: '',
      dialogFormVisible: false,
      choices: true
    }
  },
  created() {
    this.fetchInfo()
    this.adminList()
  },
  methods: {
    fetchInfo() {
      this.infoLoading = true
      const that = this
      const params = {

      }
      getToDoNotification(params, 'get')
        .then(response => {
          const data = [...response.data]
          data.forEach(function(val, index, arr) {
            val.is_open = val.is_open == 1
            val.dialogFormVisible = false
            val.choices = true
            that.list.push(val)
          })
          this.infoLoading = false
        })
        .catch(() => {})
    },
    adminList(){
      this.infoLoading = true
      const that = this
      const params = {

      }
      getAdminList(params, 'get')
        .then(response => {
          const data = [...response.data]
          that.options = []
          data.forEach(function(val, index, arr) {
            val.is_open = val.is_open == 1
            that.options.push(val)
          })
          this.infoLoading = false
        })
        .catch(() => {})
    },
    choice(id){
      this.list.forEach(function (val, index, arr) {
        if (val.id == id){
          arr[index].dialogFormVisible = true
          arr[index].choices = false
        }
      })
    },
    cancel(id){
      this.list.forEach(function (val, index, arr) {
        if (val.id == id){
          arr[index].dialogFormVisible = false
          arr[index].choices = true
        }
      })
    },
    preservation(id, tag){
      if (tag.length <= 0){
        tag = ''
      } else {
        tag = tag.join(',')
      }
      // 解决管理员微信通知数量不对呀问题，前段统计存在问题，前段限制改为接口限制
      // if(tag.length > 10){
      //   this.$message.error('管理员最多可以添加10位')
      //   return false
      // }
      const params = {
        'notice_id': id,
        'tag': tag
      }
      setToDoNotification(params, 'post')
        .then(response => {
          this.$message.success(response.message)
          this.fetchInfo()
          this.adminList()
          this.$router.go(0)
          return true
        })
        .catch(() => {})
    },
    switchButton(id){
      const params = {
        'notice_id': id
      }
      switchButton(params, 'get')
        .then(response => {
          this.$message.success(response.message)
          this.fetchInfo()
          this.adminList()
          return true
        })
        .catch(() => {})
    }
  }
}
</script>

<style scoped>
.maxlen{
  margin-right: 10px;
  margin-left: 6px;
}
.maxlen b{
  font-weight: normal;
}
.colors{
  color: #f00;
}
.choice{
  background: #4a88f7;
  color: #fff;
  font-size: 12px;
  padding: 7px 12px;
}
.preservation{
  color: #fff;
  background-color: #67c23a;
  border-color: #67c23a;
  font-size: 12px;
  padding: 7px 12px;
  margin-left:20px;
}
.cancel{
  background: #fff;
  font-size: 12px;
  padding: 7px 12px;
  color: #606266;
  border: 1px solid #dcdfe6;
  margin-left:10px;
}
.span{
  margin-left:10px;
  background-color: #ecf5ff;
  display: inline-block;
  height: 32px;
  padding: 0 10px;
  line-height: 30px;
  font-size: 12px;
  color: #409eff;
  border: 1px solid #d9ecff;
  border-radius: 4px;
  -webkit-box-sizing: border-box;
  box-sizing: border-box;
  white-space: nowrap;
}
.el-icon-question {
  font-size: 16px;
}
.el-form-item{
width:100%;
max-width:1111px;
}
</style>
